package associations;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

public class AssociationTest {
    public static void main(String[] args) {
        Configuration conf = new Configuration().configure();
        SessionFactory sf = conf.buildSessionFactory();
        Session session = sf.openSession();
        Transaction tx = session.beginTransaction();

        StudentClass sc = new StudentClass("2113班");

        Student s1 = new Student("张三", sc);
        Student s2 = new Student("李四", sc);

        session.save(sc);
        session.save(s1);
        session.save(s2);

        tx.commit();
        session.close();
        sf.close();

    }
    @Test
    public  void OneToManyTest() {
        Configuration conf = new Configuration().configure();
        SessionFactory sf = conf.buildSessionFactory();
        Session session = sf.openSession();
        Student student1 = new Student("张三", null);
        Student student2 = new Student("李四", null);

        StudentClass sc = new StudentClass("2113班");
        sc.getStudents().add(student1);
        sc.getStudents().add(student2);

        student1.setStudentClass(sc);
        student2.setStudentClass(sc);

        session.save(sc);
        session.save(student1);
        session.save(student2);

        session.close();
        sf.close();


    }
    @Test
    public void ManyToOneTest() {
        Configuration conf = new Configuration().configure();
        SessionFactory sf = conf.buildSessionFactory();
        Session session = sf.openSession();
        Student student1 = new Student("张三", null);
        Student student2 = new Student("李四", null);
        StudentClass sc = new StudentClass("2113班");
        sc.getStudents().add(student1);
        sc.getStudents().add(student2);
        student1.setStudentClass(sc);
        student2.setStudentClass(sc);
        session.save(sc);
        session.save(student1);
        session.save(student2);
        session.close();
        sf.close();
    }
    @Test
    public void ManyToManyTest() {
        Configuration conf = new Configuration().configure();
        SessionFactory sf = conf.buildSessionFactory();
        Session session = sf.openSession();
        Student student1 = new Student("张三", null);
        Student student2 = new Student("李四", null);
        Course course1 = new Course("Java", 1000);
        Course course2 = new Course("C++", 2000);
        //student1.getCourses().add(course1);
    }
}
